close all;
clear all;
clc;
%--------------------------------------------------------------------------

methods = ['FFT2'  ; 'SIZE'; 'DCT2'; 'HIST'];
metrics = ['L2'    ; 'L2'  ; 'L2'  ; 'L1'  ];
best_ma = [ 60000  ;  23   ;  500  ;  1500 ];
worst_m = [ 2000000;  38   ;  700  ;  3500 ];
in_base = 1:9;
variations = 10;
%--------------------------------------------------------------------------

methods = cellstr(methods);
test_vec = convert_to_index_in_base(generate_test_vector(in_base));
subp1 = 3; subp2 = 4;
%--------------------------------------------------------------------------

%--------------------------------------------------------------------------
tic;

database_fft2 = generate_database('FFT2'); toc;
database_size = generate_database('SIZE'); toc;
database_dct2 = generate_database('DCT2'); toc;
database_hist = generate_database('HIST'); toc;

current = 0;
correct = 0;
for i = 1:variations
    
    curr = 1;
    test_vec = convert_to_index_in_base(generate_test_vector(variations));
    
    new_database_fft2 = cut_test_images(database_fft2, test_vec);
    new_database_size = cut_test_images(database_size, test_vec);
    new_database_dct2 = cut_test_images(database_dct2, test_vec);
    new_database_hist = cut_test_images(database_hist, test_vec);
    
    for j = 1:length(test_vec)
        current = current + 1;
        
        im_vec_fft2 = database_fft2(:, test_vec(j));
        im_vec_size = database_size(:, test_vec(j));
        im_vec_dct2 = database_dct2(:, test_vec(j));
           
        [Y_fft2 I_fft2] = best_match2(im_vec_fft2, new_database_fft2, 10, 40, 'L2');
        [Y_size I_size] = best_match2(im_vec_size, new_database_size, 10, 40, 'L2');
        [Y_dct2 I_dct2] = best_match2(im_vec_dct2, new_database_dct2, 10, 40, 'L2');
                      
        [good_fft2 avr_fft2] = get_good_avg_vec(Y_fft2, I_fft2, 600000, 2000000);
        [good_size avr_size] = get_good_avg_vec(Y_size, I_size, 23, 38);
        [good_dct2 avr_dct2] = get_good_avg_vec(Y_dct2, I_dct2, 500, 700);
 
        best_img = optimize_results(good_fft2, good_size, good_dct2, avr_fft2, avr_size, avr_dct2);
        
        %czy wytypowano obrazy do drugiej rundy
        if(isempty(best_img))
            disp('brak obrazu w bazie');
            continue;
        end
        
        im_vec_hist = [];
        for k=1:length(best_img)
            im_vec_hist = new_database_hist(:, best_img(k));
        end
        
        [Y_hist I_hist] = best_match2(im_vec_hist, new_database_hist, 10, 40, 'L1');
        [dd ii] = get_dir_and_file(test_vec(j), 10);
        [d1 i1] = get_dir_and_file(I_hist(1), 10);
        
        c=false;
        if(d1 == dd)
            c = true;
            correct = correct + 1;
        end
        
        figure(1); clf;
        best = best_five(c, I_hist);
        show_image(image_read(dd, ii, 'pgm'), subp1, subp2, 1, ['testowy: ' num2str(current)]);
        show_image(best, subp1, subp2, 5:6, ['dopasowane' num2str( (correct/current)*100 ) '%']);    
        
        
%         [d_hist i_hist] = get_dir_and_file(I_hist(1), 10);
%         
%         figure(1); clf;
%         show_image(image_read(dd, ii, 'pgm'), subp1, subp2, 1, ['obraz: ' num2str(curr)]);
%         show_image(image_read(d_hist, i_hist, 'pgm'), subp1, subp2, 2, [': ' num2str(curr)]);
%         
%         if(length(I_hist) > 2)
%             [d_hist2 i_hist2] = get_dir_and_file(I_hist(2), 10);
%             show_image(image_read(d_hist2, i_hist2, 'pgm'), subp1, subp2, 3, [': ' num2str(curr)]);
%         end

%             clc;
%             good_fft2
%             avr_fft2
%             
%             good_size
%             avr_size
%             
%             good_dct2
%             avr_dct2
%             
%             size(indexes)
%             indexes
%             pause 
 


        
    end
end